import multiprocessing as mp
import threading as th
import time 

def job(q):
    res = 0
    for i in range(1000):
        res += i+i**2+i**3
    q.put(res)
    #一个process运行完成后，会将计算结果放入q里面
    #所有process运行完后，再获取值

def multicore():
    q = mp.Queue()
    #mp.Process不能有return，结果放在Queue中
    p1 = mp.Process(target=job,args=(q,))
    p2 = mp.Process(target=job,args=(q,))
    #args只有一个参数时，后面必须有，
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    res1 = q.get()
    res2 = q.get()
    print("prcoess:",res1 + res2)

def multithread():
    q = mp.Queue()
    #mp.Process不能有return，结果放在Queue中
    t1 = th.Thread(target=job,args=(q,))
    t2 = th.Thread(target=job,args=(q,))
    #args只有一个参数时，后面必须有，
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    res1 = q.get()
    res2 = q.get()
    print("thread:",res1 + res2)

def normal():
    res = 0
    for i in range(1000):
        res += i+i**2+i**3
    print("normal:",2*res)

if __name__ == "__main__":
    st = time.time()
    normal()
    st1 = time.time()
    print("normal",st1-st)
    multicore()
    st2 = time.time()
    print("process",st2-st1)
    multithread()
    st3 = time.time()
    print("thread",st3-st2)